package com.le.tester.webpage.secKill.controller;

import com.le.tester.common.ServerResponse;
import com.le.tester.dto.OrderDetailDto;
import com.le.tester.entity.OrderInfo;
import com.le.tester.webpage.secKill.service.OrderService;
import com.le.tester.webpage.secKill.service.SecKillService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * 描述：秒杀控制层
 */
@Controller
@RequestMapping("/secKill/")
public class SecKillController {

    private static final Logger LOG = LoggerFactory.getLogger(SecKillController.class);

    @Autowired
    private SecKillService secKillService;

    @Autowired
    private OrderService orderService;


    @ResponseBody
    @RequestMapping("detail")
    //获取秒杀订单详情
    public ServerResponse<OrderDetailDto> getSecKillOrderDetail(@RequestParam(value = "orderId") Long orderId) {
        //参数校验和登录校验省略

        try {
            //获取到订单信息,唯一
            OrderDetailDto orderDetailDto = secKillService.getSecKillOrderDetail(orderId);
            return ServerResponse.createBySuccess(orderDetailDto);
        } catch (Exception e) {
            LOG.info("获取秒杀订单详情异常：{}", e.getMessage());
            return ServerResponse.createByError();
        }
    }

    @ResponseBody
    @RequestMapping("doSecKill")
    public ServerResponse<Map<String, Object>> doSecKill(@RequestParam(value = "goodsId") Long goodsId) {
        //参数校验和登录校验省略
        try {
            Map<String, Object> resultMap = secKillService.doSecKill(goodsId);
            return ServerResponse.createBySuccess(resultMap);
        } catch (Exception e) {
            LOG.info("进行秒杀异常：{}", e.getMessage());
            return ServerResponse.createByError();
        }
    }

}
